package com.nanbei.doublepoint;

/**
 * @ClassDescription: 盛水最多的容器
 * @JdkVersion: 17
 * @Author: BaoShan.Li
 * @Date: 2024/7/16 23:23
 */
public class ContainerWithMostWater {
    public static void main(String[] args) {
        int i = maxArea(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7});
        System.out.println(i);
    }

    // 两块模板固定时，如果移动较长的木板则会使容器的宽度降低，使水的容量减少
    // 所以应该移动较短的木板
    public static int maxArea(int[] height) {
        //定义两个指针，下表分别为0 、length-1
        int left = 0;
        int right = height.length-1;
        int max = 0;
        // 那块模板较短则移动那块木板，然后重新计算容量取最大值
        while (left<right){
            if (height[left] < height[right]){
                int wei = (right - left) * height[left];
                max = Math.max(max,wei);
                left++;
            }else{
                int wei = (right - left) * height[right];
                max = Math.max(max,wei);
                right--;
            }
        }

        return max;
    }
}
